<template>
  <div>
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      error.sync="error"
      error-text="加载失败,请点击重试"
      @load="onLoad"
    >
      <van-cell
        v-for="(item, index) in list"
        :key="index"
        :title="item.title"
      />
    </van-list>
  </div>
</template>

<script>
import { getSearchResult } from '@/api/search'
export default {
  props: {
    searchText: {
      type: String,
      require: true
    }
  },
  components: {},
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1,
      size: 20,
      error: false
    }
  },
  created () {},
  mounted () {},
  methods: {
    async onLoad () {
      try {
        // 1. 请求获取数据
        const { data: { data: { results } } } = await
        getSearchResult({
          page: this.page, // 页码
          per_page: this.size, // 每页大小
          q: this.searchText // 查询关键词
        })
        // 2. 将数据添加到数组列表中
        this.list.push(...results)
        // 3. 将本次加载中的 loading 关闭
        this.loading = false
        // 4. 判断是否还有数据
        if (results.length === 0) {
          // 如果没有，则将加载状态 finished 设置为结束
          this.finished = false
        } else {
          // 如果有，则更新获取下一个数据的页码
          this.page++
        }
      } catch (error) {
        // 展示加载失败的提示状态
        this.error = true
        // 加载失败了 loading 也要关闭
        this.loading = true
      }
    }
  },
  watch: {},
  computed: {}
}
</script>

<style lang="less" scoped>
</style>
